#!/usr/bin/perl

######################################################################
#  Swignition/0.1-alpha15 - a toolkit for the semantic web
#  Copyright (c) 2008, 2009 Toby Inkster.
######################################################################
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
######################################################################

use utf8;
use strict;
use lib '.';
use Swignition::Misc;
use Getopt::Long
	qw(:config no_ignore_case bundling permute);
use Pod::Usage;
	
my ($givenUrl, $parser, %opts, $output_format, $help, $man, $ver, $from_stdin, $stdin, @nofollow);
my ($host, $port, $proto) = ('localhost', '26464', 'tcp');
GetOptions(
	'option|o=s%'  => \%opts,
	'format|f=s'   => \$output_format,
	'help|usage|h' => \$help,
	'man'          => \$man,
   'version|V'    => \$ver,
   'host=s'       => \$host,
   'port=i'       => \$port,
   'proto=s'      => \$proto,
   'nofollow=s'   => \@nofollow
);
pod2usage(-verbose => 2) && exit if defined $man;
pod2usage(-verbose => 1) && exit if defined $help;

if (defined $ver)
{
	print $Swignition::Misc::version . ' ' . $Swignition::Misc::releaseDate . "\n";
	exit;
}

$givenUrl = shift @ARGV or (pod2usage(-verbose=>1) && exit);
if ($givenUrl eq '-')
{
	$givenUrl = shift @ARGV || $Swignition::Misc::defaultBaseURI;
	while (<>) { s/^\.\r?$/ ./; $stdin .= $_; }
	$from_stdin = 1;
}
$givenUrl = "http://localhost/~tai/$1.html" if ($givenUrl =~ /^T:(.*)$/);


# First, attempt to connect to Swignition daemon.
if (length $host && length $proto && $port>0)
{
	require IO::Socket::INET;
	my $socket = IO::Socket::INET->new(
		PeerHost => $host,
		PeerPort => $port,
		Proto    => $proto,
	);
	if ($socket)
	{
		$socket->autoflush(1);	
		print $socket "SET AUTOCLOSE 1\r\n";
		print $socket "SET FORMAT $output_format\r\n" if (length $output_format);
		foreach my $k (%opts)
		{
			print $socket sprintf("SET OPTION %s %s\r\n", $k, $opts{$k})
				if (length $k && length $opts{$k});
		}
		foreach my $h (@nofollow)
		{
			print $socket sprintf("SET NOFOLLOW %s\r\n", $h)
				if (length $h);
		}
		if ($from_stdin)
		{
			print $socket "COGNIFY STDIN AS $givenUrl\r\n";
			print $socket "$stdin\r\n.\r\n";
		}
		else
		{
			print $socket "COGNIFY $givenUrl\r\n";
		}
		while(<$socket>)
		{
			print $_;
		}
		close($socket);
		exit;
	}
	else
	{
		warn("Using Swignition HTML parsing library locally. This will be slow.");
	}
}

# Otherwise, use local library (slow)
require Swignition::GenericParser;
require Swignition::Export::Contact;
require Swignition::Export::Feed;
require Swignition::Export::Location;
require Swignition::Export::Calendar;
require Swignition::Export::CalComponent;
require Swignition::Export::Recording;
require Swignition::Export::Recipe;
require HTTP::Request;

my ($pageUrl, $subjectUrl) = Swignition::Misc::url_split($givenUrl);

if ($from_stdin)
{
	$opts{base} = $pageUrl;
	$opts{nofollow} = \@nofollow;
	$parser = Swignition::GenericParser::new_by_type($stdin, \%opts);
}
else
{
	$opts{ua}       = Swignition::Misc::get_ua;
	$opts{request}  = HTTP::Request->new(GET => $pageUrl);
	$opts{response} = $opts{ua}->request($opts{request});
	$opts{nofollow} = \@nofollow;
	$parser         = Swignition::GenericParser::new_by_type($opts{response}->content, \%opts);
}
print Swignition::Misc::do_export($output_format, $parser, $subjectUrl);


############################################################################

__END__

=head1 NAME

swignition.pl
 
=head1 SYNOPSIS

Usage: swignition.pl [options] url

Options:

    --option KEY=VALUE      pass additional options to semantics extractor
    --format=FMT            specify output format
    --help, --usage         basic help message
    --man                   detailed help message
    --version               print current version number
    --host                  host name or IP address for daemon
    --port                  port for daemon
    --proto                 protocol for daemon
    --nofollow=HOST         don't recursively follow links to this host

=head1 OPTIONS

=over 8

=item B<--option>, B<-o>

Sets an additional option to pass to the semantics extraction engine. Currently
supported options:

  * erdf_strict_profiles
                 Require eRDF profile to be explicitly linked to.
                 Boolean; default on.  
  * grddl_fetch  Fetch profile URLs and parse for profileTransformation links.
                 Boolean; default off.
  * grddl_strict_profiles
                 Require GRDDL profile to be explicitly linked to.
                 Boolean; default off.
  * p_comments   Check for RDF in <!--comments-->. Boolean; default on.
  * p_erdf       Parse eRDF. Boolean; default on.
  * p_grddl      Attempt GRDDL gleaning. Boolean; default on.
  * p_http       Parse HTTP headers; default on.
  * p_metatags   Parse <meta> and <title> tags. Boolean; default on.
  * p_rdf        Support RDF embedded in XHTML. Boolean; default on.
  * p_rdfa       Parse RDFa. Boolean; default on.
  * p_rdfx       Support <link rel="meta" type="application/rdf+xml">.
                 Boolean; default on.
  * p_structure  Parse document heading structure. Boolean; default on.
  * p_uf         Support for some microformats. Boolean; default on.
  * rdfa_strict_doctype
                 Require XHTML+RDFa DOCTYPE. Boolean; default off.
  * rdfa_strict_version
                 Require <html version>. Boolean; default off.
  * rdfa_strings Convert nodes to strings using RDFa-specified method.
  * uf_strict_profiles
                 Require microformat profile to be explicitly linked to.
                 Boolean; default off.

Boolean options should be set to '0' (off) or '1' (on). Options are
case-sensitive.

Example:
swignition.pl -o p_erdf=1 -o p_structure=0 [url]

=item B<--format>, B<-f>

Set output format. Currently supported formats:

  * atom       Output first feed found as Atom 1.0 (RFC 4287).
  * ics        Output events as iCalendar (RFC 2445).
  * jcard      Output contacts as jCard.
  * kml        Output locations as Keyhole Markup Language (Google/OGC).
  * m3u        Output recordings as M3U playlist.
  * n3         Output as Notation3.
  * rdf-json   Output as RDF/JSON (Talis).
  * recipebook Output recipes as RecipeBook XML.
  * trix       Output as TriX.
  * turtle     Output as Turtle.
  * uf-json    Output microformats as JSON.
  * vcf        Output contacts as vCard (RFC 2426).
  * xml        Output as RDF/XML.

Format strings are case-insensitive.

=item B<--host>, B<--port>, B<--proto>

These default to 'localhost', '26464' and 'tcp' respectively. Alternative
values for proto include 'udp' and 'unix'.

The swignition command-line tool will attempt to use a daemonised copy of
swignition (a.k.a. swignitiond), and only if that can't be found will it then
try to parse the URL itself. To prevent Swignition from looking for swignitiond,
set port to -1.

Generally, swignitiond will provide a significant speed-up, as the Swignition
library will not need to be reparsed by Perl.

=item B<--help>, B<--usage>, B<--man>, B<--version>, B<-h>, B<-V>

Various forms of help. Hopefully self-explanatory.

=back

=head1 DESCRIPTION

Swignition aims to be a full-fledged graphical semantic web browser. At the
moment it is a command-line semantic web tool, able to extract semantic data
from HTML files and output it in several formats.
 
=head1 AUTHOR

Toby Inkster <http://tobyinkster.co.uk/>.
 
=head1 CREDITS

This project makes extensive use of the GNOME XML library (libxml2) and the
related XSLT library (libxslt). 

    <http://xmlsoft.org/contribs.html>
    <http://xmlsoft.org/XSLT/contribs.html>
	  
The project also makes use of LibWWW, developed by the World Wide Web
Consortium.

    <http://www.w3.org/Library/>

Numerous suggestions have been gratefully received from contributors to the
W3C semantic web mailing list and Microformats discussion list.

    <http://lists.w3.org/Archives/Public/semantic-web/>
    <http://microformats.org/mailman/listinfo/microformats-discuss/>

=head1 SEE ALSO

Semantic Web <http://www.w3.org/2001/sw/>.
Microformats <http://microformats.org/>.
Buzzword UK <http://buzzword.org.uk/>.
Swignition Services <http://srv.buzzword.org.uk/>.

=head1 BUGS

Yes.
 
